package com.boang.bank;

import java.util.concurrent.locks.ReentrantLock;

public class BankAccount {
    private final String accountNumber;
    private double balance;
    private final ReentrantLock lock = new ReentrantLock();

    public BankAccount(String accountNumber, double initialBalance) {
        this.accountNumber = accountNumber;
        this.balance = initialBalance;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public double getBalance() {
        lock.lock();
        try {
            return balance;
        } finally {
            lock.unlock();
        }
    }

    public void deposit(double amount) {
        lock.lock();
        try {
            if (amount > 0) {
                balance += amount;
            } else {
                throw new IllegalArgumentException("Deposit amount must be positive.");
            }
        } finally {
            lock.unlock();
        }
    }

    public void withdraw(double amount) throws InsufficientFundsException {
        lock.lock();
        try {
            if (amount > 0) {
                if (balance >= amount) {
                    balance -= amount;
                } else {
                    throw new InsufficientFundsException("Insufficient funds for this withdrawal.");
                }
            } else {
                throw new IllegalArgumentException("Withdrawal amount must be positive.");
            }
        } finally {
            lock.unlock();
        }
    }
}